define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'editable', 'echarts', 'echarts-theme'], function ($, undefined, Backend, Table, Form, undefined, Echarts, undefined) {

    var Controller = {
        index: function () {
            // 初始化表格參數配置
            Table.api.init({
                extend: {
                    index_url: 'jdy/sdblog/index',
                    add_url: 'jdy/sdblog/add',
                    edit_url: 'jdy/sdblog/edit',
                    del_url: 'jdy/sdblog/del',
                    multi_url: 'jdy/sdblog/multi',
                    table: 'jdy_sdblog',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                showToggle: false,
                showColumns: true,
                //showExport: false,
                searchFormVisible: true,
                columns: [
                    [
                        //{checkbox: true},
                        //{field: 'id', title: __('Id')},
                        //{field: 'sdbiao_id', title: __('Sdbiao_id')},
                        {field: 'cbs', title: __('Cbs'), operate:false},
                        {field: 'xcbs', title: __('Xcbs'), operate:false},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'sdbiao.id', title: __('Sdbiao.id')},
                        //{field: 'sdbiao.anchang_id', title: __('Sdbiao.anchang_id')},
                        //{field: 'sdbiao.type', title: __('Sdbiao.type')},
                        //{field: 'sdbiao.name', title: __('Sdbiao.name')},
                        //{field: 'sdbiao.biaohao', title: __('Sdbiao.biaohao')},
                        //{field: 'sdbiao.images', title: __('Sdbiao.images'), formatter: Table.api.formatter.images},
                        //{field: 'sdbiao.create_time', title: __('Sdbiao.create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'sdbiao.update_time', title: __('Sdbiao.update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 為表格綁定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        chart: function () {
            $("#c-submit").click(function (e) {
                //不是所有的input都提交
                //Backend.api.toastr.success("提交");
                var formdata = $("#search-form").serialize();
                if (!formdata) {
                    Backend.api.toastr.error("請選擇日期範圍");
                    return;
                }
                var x = $("#search-form").serializeArray();
                var row = new Array();
                $.each(x, function (i, field) {
                    row[field.name] = field.value;
                    if (field.name == "dateInfo") {

                    }
                });

                var url = 'jdy/sdblog/chart/ids/' + Statsdata.ids + '?ref=addtabs';
                Backend.api.ajax({
                    url: url,
                    data: formdata
                }, function (data, ret) {
                    console.log(data);
                    //Backend.api.toastr.success(data);
                    Statsdata.xdata = data.xdata;
                    Statsdata.ydata = data.ydata;
                    Controller.showechart();
                });

            });

            Controller.showechart();

            Controller.api.bindevent();
        },
        showechart: function () {
            Date.prototype.Format = function (fmt) {
                var o = {
                    "M+": this.getMonth() + 1, //月份
                    "d+": this.getDate(), //日
                    "H+": this.getHours(), //小時
                    "m+": this.getMinutes(), //分
                    "s+": this.getSeconds(), //秒
                    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
                    "S": this.getMilliseconds() //毫秒
                };
                if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
                for (var k in o)
                    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                return fmt;
            };

            var myChart = Echarts.init(document.getElementById('echart'), 'walden');

            option = {
                tooltip : {
                    trigger: 'axis',
                    axisPointer : {            // 坐標軸指示器，坐標軸觸發有效
                        type : 'shadow'        // 默認為直線，可選為：'line' | 'shadow'
                    },
                    //formatter: function (params) {
                    //    var tar = params[1];
                    //    return tar.name + '<br/>' + tar.seriesName + ' : ' + tar.value;
                    //}
                },
                grid: {
                    bottom: 66,
                    containLabel: true
                },
                title: {
                    text: Statsdata.row.anchangname + ">" + Statsdata.row.name + "[" + Statsdata.row.biaohao + "]",
                    left: 'center'
                },
                xAxis: {
                    type: 'category',
                    splitLine: {show:false},
                    data: Statsdata.xdata
                },
                yAxis: {
                    type: 'value'
                },
                dataZoom: [{
                    type: 'inside'
                }, {
                    type: 'slider'
                }],
                series: [{
                    data: Statsdata.ydata,
                    type: 'bar',
                    label: {
                        normal: {
                            show: true,
                            position: 'inside'
                        }
                    },
                }]
            };

            myChart.setOption(option);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});